package com.bjypc.youth.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.bjypc.youth.dao.BaseDaoImpl;
import com.bjypc.youth.dao.IResearchArticleDao;
import com.bjypc.youth.entity.ResearchArticle;

public class ResearchArticleDaoImpl extends BaseDaoImpl<ResearchArticle>
		implements IResearchArticleDao {
	@PersistenceContext(unitName = "bjyouth")
	private EntityManager em;

	public ResearchArticleDaoImpl() {
		super();
	}

	@PersistenceContext
	public void setEntityManager(EntityManager em) {
		this.em = em;
	}

	private EntityManager getEntityManager() {
		return em;
	}

	@Override
	public List<ResearchArticle> findAll() {
		List<ResearchArticle> articles = new ArrayList<ResearchArticle>();
		Iterator results = em
				.createQuery(
						"select id,title,author,type,subContent,suffix,publishDate  from ResearchArticle r")
				.getResultList().iterator();
		articles = getArticleVO(results);
		return articles;

	}

	public static List<ResearchArticle> getArticleVO(Iterator iterator) {
		List<ResearchArticle> articles = new ArrayList<ResearchArticle>();
		while (iterator.hasNext()) {
			Object[] row = (Object[]) iterator.next();
			Long id = (Long) row[0];
			String title = (String) row[1];
			String author = (String) row[2];
			String type = (String) row[3];
			String subContent = (String) row[4];
			String suffix = (String) row[5];
			Date publishDate = (Date) row[6];
			ResearchArticle researchArticle = new ResearchArticle();
			researchArticle.setId(id);
			researchArticle.setTitle(title);
			researchArticle.setAuthor(author);
			researchArticle.setType(type);
			researchArticle.setSubContent(subContent);
			researchArticle.setPublishDate(publishDate);
			researchArticle.setSuffix(suffix);
			articles.add(researchArticle);
		}
		return articles;
	}

	public ResearchArticle findByTitle(String md5) {
		ResearchArticle researchArticle = null;
		List resultList = this.em
				.createQuery(
						"select id from ResearchArticle article where article.md5 =:Title")
				.setParameter("Title", md5).getResultList();
		if (null != resultList && resultList.size() != 0) {
			// only one
			if (resultList.size() == 1) {
				Object[] row = (Object[]) resultList.get(0);
				Long id = (Long) row[0];
				researchArticle = this.findById(id);
			} 
		}
		return researchArticle;
	}

	public int countSameTitleArticle(String title) {
		int count = 0;

		List resultList = this.em
				.createQuery(
						"select count(id) from ResearchArticle article where article.title =:Title")
				.setParameter("Title", title).getResultList();
		if (null != resultList && resultList.size() != 0) {
			count = resultList.size();
		}
		return count;
	}

	public void removeAll() {
		this.em.createQuery("delete from ResearchArticle").executeUpdate();
	}

}
